package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.sl.au.entity.WholeSetPrice;
import com.sl.au.service.WholeSetPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sl.au.entity.Account;
import com.sl.au.entity.Module;
import com.sl.au.entity.WholeSetDevice;
import com.sl.au.service.AttachmentService;
import com.sl.au.service.WholeSetDeviceService;

@RestController
@RequestMapping(value = "/ui/priceBook/equipment")
public class WholeSetPriceController extends BaseController {

	@Autowired
	WholeSetPriceService wholeSetPriceService;

	@Autowired
	WholeSetDeviceService wholeSetDeviceService;

	@Autowired
	AttachmentService attachmentService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = createLayoutView("priceBook/equipment/equipmentIndex");
		Account a = this.getMyAccount();
		String superior = a.getUser().getSaleArea().getSuperior();
		String name="equipment"; 
        Module module = this.getPageModule(name);
        modelAndView.addObject("module", module);
		modelAndView.addObject("superior", superior);
		return modelAndView;
	}

	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		String superior = request.getParameter("superior");
		Account a = this.getMyAccount();
		superior = a.getUser().getSaleArea().getSuperior();
		String name="equipment"; 
	    Module module = this.getPageModule(name);
	    String dataFilter=module.getDataFilter();
		List<WholeSetPrice> prices = wholeSetPriceService.getEquipments(start, length, key, superior,dataFilter, null, null);
		long count = 0;
		if(dataFilter.equals("所有")){
        	count = wholeSetPriceService.getRepository().getCount(key);
        }else if(dataFilter.equals("本部门")){
        	count = wholeSetPriceService.getRepository().getCount(superior, key); 
        }
		map.put("data", prices);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Map<String, Object> saveSetPrice(HttpServletRequest request, @RequestBody WholeSetPrice price) {
		Map<String, Object> map = new HashMap<String, Object>();
		String setId = price.getWholeSetDevice().getId();
		WholeSetDevice d = wholeSetDeviceService.getRepository().findOne(setId);
		price.setEquipmentType(d.getType());
		price.setWholeSetDevice(d);
		wholeSetPriceService.getRepository().save(price);
		map.put("code", 0);
		return map;
	}

	/**
	 * EXCEL导入处理 *
	 */
	@RequestMapping(value = "/import", method = RequestMethod.POST)
	public Map<String, Object> importExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request,
			RedirectAttributes redirectAttributes) {

		Map<String, Object> map = new HashMap<String, Object>();

		int code = -1;
		long count = -1;
		Account a = this.getMyAccount();
		String superior = a.getUser().getSaleArea().getSuperior();
		String saveFileName = attachmentService.upload(file);
		// 文件传后，再读取EXCEL写入数据库
		if (StringUtils.hasText(saveFileName)) {
			count = wholeSetPriceService.importExcel(saveFileName, superior);
			code = 0;
		}
		map.put("code", code);
		map.put("count", count);
		return map;
	}

}
